//
//  ZHLEEUserAccount.swift
//  ZHBlog_swift
//
//  Created by ZHLEEBlander on 15/10/14.
//  Copyright © 2015年 ZHLEEBlander. All rights reserved.
//

import UIKit

class ZHLEEUserAccount: NSObject , NSCoding{

// MARK: -
// MARK: --------------属性
    var uid: String?

    var expires_in: NSTimeInterval = 0{
    
        didSet{
        
            //获取当前的时间
            let date = NSDate()
            
            //在当前的时间上,加上expires_in这么多秒,生成一个新的时间,为最大的过期时间
            expires_date = date.dateByAddingTimeInterval(expires_in)
        }
    }
    
    var expires_date: NSDate?

    var access_token: String?

    var remind_in: String?
    
    //头像
    var avatar_large: String?
    //用户昵称
    var name:String?
    
    override var description: String {
    
        let keys = ["uid","expires_date","access_token","remind_in","avatar_large","name"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
// MARK: - 
// MARK:-----------方法
    init(dictionary: [String : AnyObject]){
    
        super.init()
        
        setValuesForKeysWithDictionary(dictionary)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    //归档解档
    //1.获取路径
    static let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.archive")
    
    /**
    *  保存账户
    */
    func saveAccount(){
    
        printLog("归档路径:\(ZHLEEUserAccount.path)")
        
        NSKeyedArchiver.archiveRootObject(self, toFile: ZHLEEUserAccount.path)
    }
    /**
    *  读取用户
    */
    class func loadAccount()->ZHLEEUserAccount?{
        
        let account = NSKeyedUnarchiver.unarchiveObjectWithFile(self.path) as? ZHLEEUserAccount
        
        if let acc = account{
        
            //拿着过期时间与当前时间进行对比
            //如果过期时间大于当前时间.代表没有过期.否则代表过期
            if acc.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
            
                return account
            }
        }
        
        return nil
    }
    /**
    *  归档
    */
    func encodeWithCoder(encoder: NSCoder){
    
        encoder.encodeObject(access_token, forKey: "access_token")
        encoder.encodeObject(uid, forKey: "uid")
//        encoder.encodeObject(expires_in, forKey: "expires_in")
        encoder.encodeObject(expires_date, forKey: "expires_date")
        encoder.encodeObject(remind_in, forKey: "remind_in")
        encoder.encodeObject(avatar_large, forKey: "avatar_large")
        encoder.encodeObject(name, forKey: "name")
    }
    /**
    *  解档
    */
    required init?(coder aDecoder: NSCoder){
    
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
//        expires_in = aDecoder.decodeIntegerForKey("expires_in")
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        remind_in = aDecoder.decodeObjectForKey("remind_in") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        name = aDecoder.decodeObjectForKey("name") as? String

    }
    
    
}
